package com.elvish.efast.modules.sys.controller;

import com.elvish.efast.common.anno.NeedPage;
import com.elvish.efast.common.consts.Const;
import com.elvish.efast.common.entity.Page;
import com.elvish.efast.common.entity.Result;
import com.elvish.efast.common.entity.TableResponse;
import com.elvish.efast.common.enums.ResultCode;
import com.elvish.efast.common.utils.CommonUtil;
import com.elvish.efast.common.utils.ResultUtil;
import com.elvish.efast.modules.sys.entity.Dict;
import com.elvish.efast.modules.sys.service.DictService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.nutz.lang.Lang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * (TDict)表控制层
 *
 * @author makejava
 * @since 2018-11-13 13:30:59
 */
@RestController
@RequestMapping("/dict")
@Slf4j
public class DictController {
    /**
     * 服务对象
     */
    @Autowired
    private DictService tDictService;

    /**
     * 查询字典列表
     *
     * @param page 分页信息
     * @param dict 查询参数
     * @return 响应消息
     */
    @ApiOperation(value = "查询字典列表", notes = "查询字典列表 = 分页信息 + 查询参数")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "分页信息", required = true, dataType = "Page"),
        @ApiImplicitParam(name = "dict", value = "字典信息", dataType = "Dict")
    })
    @GetMapping("/queryAll")
    @NeedPage
    public TableResponse queryAll(Page page, Dict dict) {
        return TableResponse.builder().data(tDictService.queryAll(dict)).build();
    }

    /**
     * 编辑字典信息
     * @param oper 操作符
     * @param dict 字典信息
     * @return 响应消息
     */
    @ApiOperation(value = "编辑字典信息", notes = "编辑字典信息 = 操作符 + 字典信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "oper", value = "操作符", required = true, dataType = "Integer"),
        @ApiImplicitParam(name = "dict", value = "字典信息", dataType = "Dict")
    })
    @PostMapping("/editDict")
    public Result editDict(int oper, @RequestBody Dict dict) {
        Result result = null;
        try {
            if (oper == Const.OperationType.ADD) {
                dict.setId(CommonUtil.uuid());
                tDictService.insert(dict);
            } else if (oper == Const.OperationType.UPDATE) {
                tDictService.update(dict);
            } else if (oper == Const.OperationType.DELETE) {
                String[] ids = dict.getIds();
                if (Lang.isEmpty(ids)) {
                    ids = new String[1];
                    ids[0] = dict.getId();
                }
                tDictService.deleteByIds(ids);
            }
            result = ResultUtil.success();
        } catch (Exception e) {
            log.error("editDict error", e);
            result = ResultUtil.fail(ResultCode.ERROR);
        }
        return result;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    public Dict selectOne(String id) {
        return this.tDictService.queryById(id);
    }

}